﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

    /// <summary>
    /// Random 3x3 or 4x4(depending on difficulty) filled with small numbers(0-9)
    /// players have 5s?? to estimate the sum of the elements
    /// after 5s console clear console ask player for result
    ///
    /// </summary>
class MatrixCalculator : Game
{
    public MatrixCalculator(int diff)
        : base(diff)
    { }
    int Answer, UserAnswer;
    public override void StartGame()
    {
        MatrixRandom();
        int Size = MatrixRandom().GetLength(1);
        int[,] WorkMatrix = new int[Size, Size];
        WorkMatrix = MatrixRandom();
        GameStart();
        Console.Clear();
        PrintMatrix(WorkMatrix);
        Answer = SumMatrix(WorkMatrix);
        Thread.Sleep(5000);
        Console.Clear();
        InputAnswer();
        EndGame();

    }
    private int[,] MatrixRandom() //Method for generating the matrix
    {
        int RowCol;
        if (difficulty >= 5)//Difficulty equal more or less rows and columns
        {
            RowCol = 4;
        }
        else
        {
            RowCol = 3;
        }
        int[,] ReturnMatrix = new int[RowCol, RowCol];
        Random rand = new Random();
        for (int i = 0; i < RowCol; i++)
        {
            for (int z = 0; z < RowCol; z++)
            {
                ReturnMatrix[i, z] = rand.Next(1, 10);
            }
        }
        return ReturnMatrix;
    }
    private void PrintMatrix(int[,] Print)//Method for printing the matrix
    {
        for (int i = 0; i < Print.GetLength(1); i++)
        {
            for (int z = 0; z < Print.GetLength(1); z++)
            {
                Console.Write(Print[i, z]);
                Console.Write(" ");

            }
            Console.WriteLine();
        }
        Console.WriteLine("Please dont enter any numbers before being asked to");
    }
    private int SumMatrix(int[,] Sum)//Sum of the elements of the Matrix
    {
        int result = 0;
        for (int i = 0; i < Sum.GetLength(1); i++)
        {
            for (int z = 0; z < Sum.GetLength(1); z++)
            {
                result += Sum[i, z];
            }

        }
        return result;
    }
    private void GameStart()//Explanation before start of test
    {
        Console.WriteLine("You have 5 seconds to memorize the elements of the matrix");
        Console.WriteLine("After 5 seconds the matrix will be deleted from the console");
        Console.WriteLine("You will be asked to input the sum of the elements of the matrix");
        Console.WriteLine("To begin the game press any key");
        Console.ReadKey();
    }
    private void InputAnswer()//Getting input from user
    {
        Console.Write("Please enter your answer:");
        try
        {
            UserAnswer = int.Parse(Console.ReadLine());
        }
        catch(Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
    public override void EndGame() //Display EndGame message with score and right answers and input
    {
       
        Console.Clear();
        if (UserAnswer == Answer)
        {
            Console.WriteLine("Congratulations your answer is correct:{0}", UserAnswer);
            Console.WriteLine("Your result is 100 points");
            Console.WriteLine("Press any key to continue");
            result = 100;
            Console.ReadKey();
        }
        else if (UserAnswer > Answer && (UserAnswer - Answer) < 10)
        {
            Console.WriteLine("Sorry but your answer {0} is incorrect.", UserAnswer);
            Console.WriteLine("The correct answer is:{0}", Answer);
            Console.WriteLine("Your result is 50 points");
            Console.WriteLine("Press any key to continue");
            result = 50;
            Console.ReadKey();
        }
        else if (UserAnswer > Answer && (UserAnswer - Answer) > 10)
        {
            Console.WriteLine("Sorry but your answer {0} is quite incorrect.", UserAnswer);
            Console.WriteLine("The correct answer is:{0}", Answer);
            Console.WriteLine("Your result is 10 points");
            Console.WriteLine("Press any key to continue");
            result = 10;
            Console.ReadKey();
        }
        else if (UserAnswer < Answer && (Answer - UserAnswer) < 10)
        {
            Console.WriteLine("Sorry but your answer {0} is  incorrect.", UserAnswer);
            Console.WriteLine("The correct answer is:{0}", Answer);
            Console.WriteLine("Your result is 50 points");
            Console.WriteLine("Press any key to continue");
            result = 50;
            Console.ReadKey();
        }
        else if (UserAnswer < Answer && (Answer - UserAnswer) > 10)
        {
            Console.WriteLine("Sorry but your answer {0} is incorrect.", UserAnswer);
            Console.WriteLine("The correct answer is:{0}", Answer);
            Console.WriteLine("Your result is 50 points");
            Console.WriteLine("Press any key to continue");
            result = 50;
            Console.ReadKey();
        }

    }

}
